﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Enums.Base;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.InsuranceProducts;
using ZBJF.Warranty.Model.ViewModel.Order;

namespace ZBJF.Warranty.BLL
{
   public partial class CellPhoneInsuranceInfoService
    {

        #region 获取保单服务项目的可维修配件等级列表 + List<RepairAccessoriesLevelInfo> GetRepairAccessoriesLevelInfo(long insuranceProductID)
        /// <summary>
        /// 获取保单服务项目的可维修配件等级列表
        /// </summary>
        /// <param name="insuranceProductID">保险产品服务项目ID</param>
        /// <returns></returns>
        public List<RepairAccessoriesLevelInfo> GetRepairAccessoriesLevelInfo(int cellPhoneServiceProjectID)
        {
            return (from a in ServiceFactory.InsuranceProductScreenSPsSnapshootServiceInstance.LoadEntities(t => t.InsuranceOrderServiceProjectId == cellPhoneServiceProjectID && t.ServiceProjectType == (byte)ServiceProductType.CellPhoneRepair)
                    join b in ServiceFactory.AccessoriesLevelServiceInstance.LoadEntities(t => true) on a.AccessoriesLevelID equals b.ID into temp1
                    from c in temp1.DefaultIfEmpty()
                    join d in ServiceFactory.AccessoriesTypeServiceInstance.LoadEntities(t => true) on c.AccessoriesTypeID equals d.ID into temp2
                    from e in temp2.DefaultIfEmpty()
                    join f in ServiceFactory.ProductTypeServiceInstance.LoadEntities(t => true) on e.ProductTypeID equals f.ID into temp3
                    from g in temp3.DefaultIfEmpty()
                    select new RepairAccessoriesLevelInfo
                    {
                        AccessoriesLevelName = c.AccessoriesLevelName,
                        AccessoriesTypeName = e.AccessoriesName,
                        ProductTypeName = g.ProductName
                    }).ToList();
        }
        #endregion

        #region 获取保险产品手机服务项目信息 + List<CellPhoneInsuranceInfo> LoadByIMEI(string IMEI)
        /// <summary>
        /// 获取保险产品手机服务项目信息(已审核通过、已生效的)
        /// </summary>
        /// <param name="IMEI"></param>
        /// <param name="companyID">保险公司ID</param>
        /// <returns></returns>
        public List<CellPhoneInsuranceInfoModel> LoadByIMEI(string IMEI, long companyID)
        {
            return (from a in LoadEntities(t => t.IMEI == IMEI)
                    join b in ServiceFactory.InsuranceOrderServiceInstance.LoadEntities(t => true) on a.InsuranceOrderID equals b.ID
                    join c in ServiceFactory.InsuranceCompanyServiceInstance.LoadEntities(t => t.ParentID == companyID) on b.InsuranceCompanyID equals c.ID
                    select new CellPhoneInsuranceInfoModel()
                    {
                        InsuranceOrderID = b.ID,
                        OrderStatus = b.OrderStatus
                    }).ToList();
        }
        #endregion

        #region 获取保险产品服务项目IMEI号或苹果唯一编号 + GetIMEIOrIDFV(long insuranceOrderModelID)

        /// <summary>
        /// 获取保险产品服务项目IMEI号或苹果唯一编号
        /// </summary>
        /// <param name="insuranceOrderModelID"></param>
        /// <returns></returns>
        public string GetIMEIOrIDFV(long insuranceOrderModelID)
        {
            var re = LoadEntities(t => t.InsuranceOrderID == insuranceOrderModelID).OrderByDescending(t=>t.ID).FirstOrDefault();
            if (re == null)
                return "";
            if (!string.IsNullOrWhiteSpace(re.IMEI))
                return re.IMEI;
            else
                return re.IDFV;
        }
        #endregion
    }
}
